
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>socket.io-client API · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    
    <link rel="prev" href="emit.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="socket.io-api.html">
            
                <a href="socket.io-api.html">
            
                    
                    socket.io API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="emit.html">
            
                <a href="emit.html">
            
                    
                    emit
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.4" data-path="socket.io-client-api.html">
            
                <a href="socket.io-client-api.html">
            
                    
                    socket.io-client API
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >socket.io-client API</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="table-of-contents">Table of Contents</h2>
<ul>
<li><a href="#io">IO</a><ul>
<li><a href="#ioprotocol">io.protocol</a></li>
<li><a href="#iourl-options">io(url[, options])</a></li>
<li><a href="#manager">Class: io.Manager</a><ul>
<li><a href="#new-managerurl-options">new Manager(url[, options])</a></li>
<li><a href="#managerreconnectionvalue">manager.reconnection([value])</a></li>
<li><a href="#managerreconnectionattemptsvalue">manager.reconnectionAttempts([value])</a></li>
<li><a href="#managerreconnectiondelayvalue">manager.reconnectionDelay([value])</a></li>
<li><a href="#managerreconnectiondelaymaxvalue">manager.reconnectionDelayMax([value])</a></li>
<li><a href="#managertimeoutvalue">manager.timeout([value])</a></li>
<li><a href="#manageropencallback">manager.open([callback])</a></li>
<li><a href="#managerconnectcallback">manager.connect([callback])</a></li>
<li><a href="#managersocketnsp-options">manager.socket(nsp, options)</a></li>
<li><a href="#event-connect_error">Event: &apos;connect_error&apos;</a></li>
<li><a href="#event-connect_timeout">Event: &apos;connect_timeout&apos;</a></li>
<li><a href="#event-reconnect">Event: &apos;reconnect&apos;</a></li>
<li><a href="#event-reconnect_attempt">Event: &apos;reconnect_attempt&apos;</a></li>
<li><a href="#event-reconnecting">Event: &apos;reconnecting&apos;</a></li>
<li><a href="#event-reconnect_error">Event: &apos;reconnect_error&apos;</a></li>
<li><a href="#event-reconnect_failed">Event: &apos;reconnect_failed&apos;</a></li>
<li><a href="#event-ping">Event: &apos;ping&apos;</a></li>
<li><a href="#event-pong">Event: &apos;pong&apos;</a></li>
</ul>
</li>
<li><a href="#socket">Class: io.Socket</a><ul>
<li><a href="#socketid">socket.id</a></li>
<li><a href="#socketopen">socket.open()</a></li>
<li><a href="#socketconnect">socket.connect()</a></li>
<li><a href="#socketsendargs-ack">socket.send([...args][, ack])</a></li>
<li><a href="#socketemiteventname-args-ack">socket.emit(eventName[, ...args][, ack])</a></li>
<li><a href="#socketoneventname-callback">socket.on(eventName, callback)</a></li>
<li><a href="#socketcompressvalue">socket.compress(value)</a></li>
<li><a href="#socketclose">socket.close()</a></li>
<li><a href="#socketdisconnect">socket.disconnect()</a></li>
<li><a href="#event-connect">Event: &apos;connect&apos;</a></li>
<li><a href="#event-connect_error-1">Event: &apos;connect_error&apos;</a></li>
<li><a href="#event-connect_timeout-1">Event: &apos;connect_timeout&apos;</a></li>
<li><a href="#event-error">Event: &apos;error&apos;</a></li>
<li><a href="#event-disconnect">Event: &apos;disconnect&apos;</a></li>
<li><a href="#event-reconnect-1">Event: &apos;reconnect&apos;</a></li>
<li><a href="#event-reconnect_attempt-1">Event: &apos;reconnect_attempt&apos;</a></li>
<li><a href="#event-reconnecting-1">Event: &apos;reconnecting&apos;</a></li>
<li><a href="#event-reconnect_error-1">Event: &apos;reconnect_error&apos;</a></li>
<li><a href="#event-reconnect_failed-1">Event: &apos;reconnect_failed&apos;</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h3 id="io">IO</h3>
<p>&#x5728;&#x72EC;&#x7ACB;&#x6784;&#x5EFA;&#x4E2D;&#x4F5C;&#x4E3A;<code>io&apos;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x66B4;&#x9732;&#xFF0C;&#x6216;&#x8005;&#x8C03;&#x7528;</code>require&#xFF08;&apos;socket.io-client&apos;&#xFF09;&apos;&#x7684;&#x7ED3;&#x679C;&#x3002;</p>
<h4 id="ioprotocol">io.protocol</h4>
<ul>
<li><em>(Number)</em></li>
</ul>
<p>&#x534F;&#x8BAE;&#x7248;&#x672C;&#x53F7;&#x3002;</p>
<h4 id="iourl-options">io(url[, options])</h4>
<ul>
<li><code>url</code> <em>(String)</em></li>
<li><code>options</code> <em>(Object)</em></li>
<li><strong>Returns</strong> <code>Socket</code></li>
</ul>
<p>&#x4E3A;&#x7ED9;&#x5B9A;&#x7684;URL&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684;<code>Manager</code>&#xFF0C;&#x5E76;&#x5C1D;&#x8BD5;&#x91CD;&#x7528;&#x4E00;&#x4E2A;&#x73B0;&#x6709;&#x7684;<code>Manager</code>&#x7528;&#x4E8E;&#x540E;&#x7EED;&#x8C03;&#x7528;&#xFF0C;&#x9664;&#x975E;<code>multiplex</code>&#x9009;&#x9879;&#x901A;&#x8FC7;<code>false</code>&#x4F20;&#x9012;&#x3002;&#x3002;&#x4F20;&#x9012;&#x8FD9;&#x4E2A;&#x9009;&#x9879;&#x76F8;&#x5F53;&#x4E8E;&#x4F20;&#x9012;<code>`force new connection&apos;&#xFF1A;true</code>&#x6216;<code>forceNew&#xFF1A;true</code>&#x3002;</p>
<p>&#x4E3A;URL&#x4E2D;&#x7684;&#x8DEF;&#x5F84;&#x540D;&#x6307;&#x5B9A;&#x7684;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x65B0;&#x7684;Socket&#x5B9E;&#x4F8B;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A;<code>/</code>&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x5982;&#x679C;<code>url</code>&#x662F;<code>http&#xFF1A;// localhost / users&apos;&#xFF0C;&#x90A3;&#x4E48;&#x5C06;&#x5EFA;&#x7ACB;&#x5230;</code>http&#xFF1A;// localhost&apos;&#x7684;&#x4F20;&#x8F93;&#x8FDE;&#x63A5;&#xFF0C;&#x5E76;&#x4E14;&#x5C06;&#x5EFA;&#x7ACB;&#x5230;/ users&#x7684;Socket.IO&#x8FDE;&#x63A5;&#x3002;</p>
<p>&#x8BF7;&#x67E5;&#x770B; <a href="#managerurl-options">new Manager(url[, options])</a> &#x9009;&#x9879;</p>
<h3 id="manager">Manager</h3>
<h4 id="new-managerurl-options">new Manager(url[, options])</h4>
<ul>
<li><code>url</code> <em>(String)</em></li>
<li><code>options</code> <em>(Object)</em><ul>
<li><code>path</code> <em>(String)</em> &#x5728;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x6355;&#x83B7;&#x7684;&#x8DEF;&#x5F84;&#x540D;&#x79F0;&#xFF08;<code>/ socket.io</code>&#xFF09;</li>
<li><code>reconnection</code> <em>(Boolean)</em> &#x662F;&#x5426;&#x81EA;&#x52A8;&#x91CD;&#x65B0;&#x8FDE;&#x63A5;&#xFF08;<code>true</code>&#xFF09;</li>
<li><code>reconnectionAttempts</code> <em>(Number)</em> &#x653E;&#x5F03;&#x524D;&#x7684;&#x91CD;&#x8FDE;&#x5C1D;&#x8BD5;&#x6B21;&#x6570;&#xFF08;`Infinity&apos;&#xFF09;</li>
<li><code>reconnectionDelay</code> <em>(Number)</em> &#x5728;&#x5C1D;&#x8BD5;&#x65B0;&#x7684;&#x524D;&#x6700;&#x521D;&#x7B49;&#x5F85;&#x591A;&#x957F;&#x65F6;&#x95F4;
reconnection (<code>1000</code>). &#x53D7;+/-<code>randomizationFactor</code>&#x5F71;&#x54CD;&#xFF0C;
&#x4F8B;&#x5982;&#x9ED8;&#x8BA4;&#x521D;&#x59CB;&#x5EF6;&#x8FDF;&#x5C06;&#x5728;500&#x5230;1500ms&#x4E4B;&#x95F4;&#x3002;</li>
<li><code>reconnectionDelayMax</code> <em>(Number)</em> &#x6700;&#x5927;&#x7B49;&#x5F85;&#x65F6;&#x95F4;&#x95F4;&#x9694;
&#x91CD;&#x8FDE;&#xFF08;<code>5000</code>&#xFF09;&#x3002;&#x6BCF;&#x6B21;&#x5C1D;&#x8BD5;&#x90FD;&#x5C06;&#x91CD;&#x65B0;&#x8FDE;&#x63A5;&#x5EF6;&#x8FDF;&#x589E;&#x52A0;2&#x500D;&#x4EE5;&#x53CA;&#x5982;&#x4E0A;&#x6240;&#x8FF0;&#x7684;&#x968F;&#x673A;&#x5316;</li>
<li><code>randomizationFactor</code> <em>(Number)</em> (<code>0.5</code>), 0 &lt;= randomizationFactor &lt;= 1</li>
<li><code>timeout</code> <em>(Number)</em> &#x8FDE;&#x63A5;&#x8D85;&#x65F6;&#x524D;&#x4E00;&#x4E2A;<code>connect_error</code>
&#x548C;<code>connect_timeout</code>&#x4E8B;&#x4EF6;&#x88AB;&#x53D1;&#x5C04;&#xFF08;<code>20000</code>&#xFF09;</li>
<li><code>autoConnect</code> <em>(Boolean)</em> by &#x8BBE;&#x7F6E;&#x8FD9;&#x4E2A;&#x5047;&#xFF0C;&#x4F60;&#x5FC5;&#x987B;&#x8C03;&#x7528;<code>manager.open</code>&#x6BCF;&#x5F53;&#x4F60;&#x51B3;&#x5B9A;&#x5B83;&#x662F;&#x9002;&#x5F53;&#x7684;</li>
</ul>
</li>
<li><strong>Returns</strong> <code>Socket</code></li>
</ul>
<p><code>options</code>&#x5728;&#x521D;&#x59CB;&#x5316;&#x5E95;&#x5C42;<code>Socket</code>&#x65F6;&#x4E5F;&#x88AB;&#x4F20;&#x9012;&#x7ED9;<code>engine.io-client</code>&#x3002;&#x8BF7;&#x53C2;&#x89C1;&#x53EF;&#x7528;&#x7684;<code>options</code> [here]&#xFF08;<a href="https://github.com/socketio/engine.io-client#methods&#xFF09;&#x3002;" target="_blank">https://github.com/socketio/engine.io-client#methods&#xFF09;&#x3002;</a></p>
<h4 id="managerreconnectionvalue">manager.reconnection([value])</h4>
<ul>
<li><code>value</code> <em>(Boolean)</em></li>
<li><strong>Returns</strong> <code>Manager|Boolean</code></li>
</ul>
<p>&#x8BBE;&#x7F6E;<code>reconnection</code>&#x9009;&#x9879;&#xFF0C;&#x6216;&#x8005;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x4F20;&#x9012;&#x53C2;&#x6570;&#x5219;&#x8FD4;&#x56DE;&#x3002;</p>
<h4 id="managerreconnectionattemptsvalue">manager.reconnectionAttempts([value])</h4>
<ul>
<li><code>value</code> <em>(Number)</em></li>
<li><strong>Returns</strong> <code>Manager|Number</code></li>
</ul>
<p>&#x8BBE;&#x7F6E;<code>reconnectionAttempts</code>&#x9009;&#x9879;&#xFF0C;&#x6216;&#x8005;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x4F20;&#x9012;&#x53C2;&#x6570;&#x5219;&#x8FD4;&#x56DE;&#x5B83;&#x3002;</p>
<h4 id="managerreconnectiondelayvalue">manager.reconnectionDelay([value])</h4>
<ul>
<li><code>value</code> <em>(Number)</em></li>
<li><strong>Returns</strong> <code>Manager|Number</code></li>
</ul>
<p>&#x8BBE;&#x7F6E;<code>reconnectionDelay</code>&#x9009;&#x9879;&#xFF0C;&#x6216;&#x8005;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x4F20;&#x9012;&#x53C2;&#x6570;&#x5219;&#x8FD4;&#x56DE;&#x5B83;&#x3002;</p>
<h4 id="managerreconnectiondelaymaxvalue">manager.reconnectionDelayMax([value])</h4>
<ul>
<li><code>value</code> <em>(Number)</em></li>
<li><strong>Returns</strong> <code>Manager|Number</code></li>
</ul>
<p>&#x8BBE;&#x7F6E;<code>reconnectionDelayMax</code>&#x9009;&#x9879;&#xFF0C;&#x6216;&#x8005;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x4F20;&#x9012;&#x53C2;&#x6570;&#x5219;&#x8FD4;&#x56DE;&#x5B83;&#x3002;</p>
<h4 id="managertimeoutvalue">manager.timeout([value])</h4>
<ul>
<li><code>value</code> <em>(Number)</em></li>
<li><strong>Returns</strong> <code>Manager|Number</code></li>
</ul>
<p>&#x8BBE;&#x7F6E;<code>timeout</code>&#x9009;&#x9879;&#xFF0C;&#x6216;&#x8005;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x4F20;&#x9012;&#x53C2;&#x6570;&#x5219;&#x8FD4;&#x56DE;&#x5B83;&#x3002;</p>
<h4 id="manageropencallback">manager.open([callback])</h4>
<ul>
<li><code>callback</code> <em>(Function)</em></li>
<li><strong>Returns</strong> <code>Manager</code></li>
</ul>
<p>&#x5982;&#x679C;&#x7BA1;&#x7406;&#x5668;&#x662F;&#x901A;&#x8FC7;<code>autoConnect</code>&#x542F;&#x52A8;&#x7684;&#xFF0C;&apos;false&apos;&#xFF0C;&#x542F;&#x52A8;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x8FDE;&#x63A5;&#x5C1D;&#x8BD5;&#x3002; <code>callback</code>&#x53C2;&#x6570;&#x662F;&#x53EF;&#x9009;&#x7684;&#xFF0C;&#x4E00;&#x65E6;&#x5C1D;&#x8BD5;&#x5931;&#x8D25;/&#x6210;&#x529F;&#x5C06;&#x88AB;&#x8C03;&#x7528;&#x3002;</p>
<h4 id="managerconnectcallback">manager.connect([callback])</h4>
<p>&#x540C; <a href="#manageropencallback">manager.open([callback])</a>.</p>
<h4 id="managersocketnsp-options">manager.socket(nsp, options)</h4>
<ul>
<li><code>nsp</code> <em>(String)</em></li>
<li><code>options</code> <em>(Object)</em></li>
<li><strong>Returns</strong> <code>Socket</code></li>
</ul>
<p>&#x4E3A;&#x7ED9;&#x5B9A;&#x7684;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684;<code>Socket</code>&#x3002;</p>
<h4 id="event-connecterror">Event: &apos;connect_error&apos;</h4>
<ul>
<li><code>error</code> <em>(Object)</em> error object</li>
</ul>
<p>&#x5728;&#x8FDE;&#x63A5;&#x9519;&#x8BEF;&#x65F6;&#x89E6;&#x53D1;&#x3002;</p>
<h4 id="event-connecttimeout">Event: &apos;connect_timeout&apos;</h4>
<p>&#x5728;&#x8FDE;&#x63A5;&#x8D85;&#x65F6;&#x65F6;&#x89E6;&#x53D1;&#x3002;</p>
<h4 id="event-reconnect">Event: &apos;reconnect&apos;</h4>
<ul>
<li><code>attempt</code> <em>(Number)</em> &#x91CD;&#x65B0;&#x8FDE;&#x63A5;&#x5C1D;&#x8BD5;&#x6B21;&#x6570;</li>
</ul>
<p>&#x5728;&#x6210;&#x529F;&#x91CD;&#x65B0;&#x8FDE;&#x63A5;&#x65F6;&#x6FC0;&#x53D1;&#x3002;</p>
<h4 id="event-reconnectattempt">Event: &apos;reconnect_attempt&apos;</h4>
<p>&#x5728;&#x5C1D;&#x8BD5;&#x91CD;&#x65B0;&#x8FDE;&#x63A5;&#x65F6;&#x6FC0;&#x53D1;&#x3002;</p>
<h4 id="event-reconnecting">Event: &apos;reconnecting&apos;</h4>
<ul>
<li><code>attempt</code> <em>(Number)</em> &#x91CD;&#x65B0;&#x8FDE;&#x63A5;&#x5C1D;&#x8BD5;&#x6B21;&#x6570;</li>
</ul>
<p>&#x5728;&#x6210;&#x529F;&#x91CD;&#x65B0;&#x8FDE;&#x63A5;&#x65F6;&#x6FC0;&#x53D1;&#x3002;</p>
<h4 id="event-reconnecterror">Event: &apos;reconnect_error&apos;</h4>
<ul>
<li><code>error</code> <em>(Object)</em> error object</li>
</ul>
<p>&#x5728;&#x91CD;&#x65B0;&#x8FDE;&#x63A5;&#x5C1D;&#x8BD5;&#x9519;&#x8BEF;&#x65F6;&#x89E6;&#x53D1;&#x3002;</p>
<h4 id="event-reconnectfailed">Event: &apos;reconnect_failed&apos;</h4>
<p>&#x5728;&#x201C;reconnectionAttempts&#x201D;&#x4E2D;&#x65E0;&#x6CD5;&#x91CD;&#x65B0;&#x8FDE;&#x63A5;&#x65F6;&#x89E6;&#x53D1;&#x3002;</p>
<h4 id="event-ping">Event: &apos;ping&apos;</h4>
<p>&#x5C06;ping&#x6570;&#x636E;&#x5305;&#x5199;&#x5165;&#x670D;&#x52A1;&#x5668;&#x65F6;&#x89E6;&#x53D1;&#x3002;</p>
<h4 id="event-pong">Event: &apos;pong&apos;</h4>
<ul>
<li><code>ms</code> <em>(Number)</em> &#x4ECE;&#x201C;ping&#x201D;&#x5305;&#x8FC7;&#x53BB;&#x7684;&#x65F6;&#x95F4;&#xFF08;&#x5373;&#xFF1A;&#x5EF6;&#x8FDF;&#xFF09;&#x3002;</li>
</ul>
<p>&#x4ECE;&#x670D;&#x52A1;&#x5668;&#x63A5;&#x6536;&#x5230;pong&#x65F6;&#x89E6;&#x53D1;&#x3002;</p>
<h3 id="socket">Socket</h3>
<h4 id="socketid">socket.id</h4>
<ul>
<li><em>(String)</em></li>
</ul>
<p>&#x5957;&#x63A5;&#x5B57;&#x4F1A;&#x8BDD;&#x7684;&#x552F;&#x4E00;&#x6807;&#x8BC6;&#x7B26;&#x3002;&#x5728;<code>connect</code>&#x4E8B;&#x4EF6;&#x88AB;&#x89E6;&#x53D1;&#x540E;&#x8BBE;&#x7F6E;&#xFF0C;&#x5E76;&#x5728;<code>reconnect</code>&#x4E8B;&#x4EF6;&#x540E;&#x66F4;&#x65B0;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> socket = io(<span class="hljs-string">&apos;http://localhost&apos;</span>);

<span class="hljs-built_in">console</span>.log(socket.id); <span class="hljs-comment">// undefined</span>

socket.on(<span class="hljs-string">&apos;connect&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
  <span class="hljs-built_in">console</span>.log(socket.id); <span class="hljs-comment">// &apos;G5p5...&apos;</span>
});
</code></pre>
<h4 id="socketopen">socket.open()</h4>
<ul>
<li><strong>Returns</strong> <code>Socket</code></li>
</ul>
<p>Opens the socket.</p>
<h4 id="socketconnect">socket.connect()</h4>
<p>Synonym of <a href="#socketopen">socket.open()</a>.</p>
<h4 id="socketsendargs-ack">socket.send([...args][, ack])</h4>
<ul>
<li><code>args</code></li>
<li><code>ack</code> <em>(Function)</em></li>
<li><strong>Returns</strong> <code>Socket</code></li>
</ul>
<p>&#x53D1;&#x9001;<code>message</code>&#x4E8B;&#x4EF6;&#x3002;&#x770B;&#x5230;<a href="#socketemiteventname-args-ack">socket.emit(eventName[, ...args][, ack])</a>.</p>
<h4 id="socketemiteventname-args-ack">socket.emit(eventName[, ...args][, ack])</h4>
<ul>
<li><code>eventName</code> <em>(String)</em></li>
<li><code>args</code></li>
<li><code>ack</code> <em>(Function)</em></li>
<li><strong>Returns</strong> <code>Socket</code></li>
</ul>
<p>&#x5C06;&#x4E00;&#x4E2A;&#x4E8B;&#x4EF6;&#x53D1;&#x9001;&#x5230;&#x7531;&#x5B57;&#x7B26;&#x4E32;&#x540D;&#x79F0;&#x6807;&#x8BC6;&#x7684;&#x5957;&#x63A5;&#x5B57;&#x3002;&#x53EF;&#x4EE5;&#x5305;&#x62EC;&#x4EFB;&#x4F55;&#x5176;&#x5B83;&#x53C2;&#x6570;&#x3002;&#x652F;&#x6301;&#x6240;&#x6709;&#x53EF;&#x4E32;&#x884C;&#x5316;&#x7684;&#x6570;&#x636E;&#x7ED3;&#x6784;&#xFF0C;&#x5305;&#x62EC;<code>Buffer</code>&#x3002;</p>
<pre><code class="lang-js">socket.emit(<span class="hljs-string">&apos;hello&apos;</span>, <span class="hljs-string">&apos;world&apos;</span>);
socket.emit(<span class="hljs-string">&apos;with-binary&apos;</span>, <span class="hljs-number">1</span>, <span class="hljs-string">&apos;2&apos;</span>, { <span class="hljs-number">3</span>: <span class="hljs-string">&apos;4&apos;</span>, <span class="hljs-number">5</span>: <span class="hljs-keyword">new</span> Buffer(<span class="hljs-number">6</span>) });
</code></pre>
<p><code>ack</code>&#x53C2;&#x6570;&#x662F;&#x53EF;&#x9009;&#x7684;&#xFF0C;&#x5C06;&#x4F7F;&#x7528;&#x670D;&#x52A1;&#x5668;&#x7B54;&#x6848;&#x8C03;&#x7528;&#x3002;</p>
<pre><code class="lang-js">socket.emit(<span class="hljs-string">&apos;ferret&apos;</span>, <span class="hljs-string">&apos;tobi&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) </span>{
  <span class="hljs-built_in">console</span>.log(data); <span class="hljs-comment">// data will be &apos;woot&apos;</span>
});

<span class="hljs-comment">// server:</span>
<span class="hljs-comment">//  io.on(&apos;connection&apos;, function (socket) {</span>
<span class="hljs-comment">//    socket.on(&apos;ferret&apos;, function (name, fn) {</span>
<span class="hljs-comment">//      fn(&apos;woot&apos;);</span>
<span class="hljs-comment">//    });</span>
<span class="hljs-comment">//  });</span>
</code></pre>
<h4 id="socketoneventname-callback">socket.on(eventName, callback)</h4>
<ul>
<li><code>eventName</code> <em>(String)</em></li>
<li><code>callback</code> <em>(Function)</em></li>
<li><strong>Returns</strong> <code>Socket</code></li>
</ul>
<p>&#x6CE8;&#x518C;&#x7ED9;&#x5B9A;&#x4E8B;&#x4EF6;&#x7684;&#x65B0;&#x5904;&#x7406;&#x7A0B;&#x5E8F;&#x3002;</p>
<pre><code class="lang-js">socket.on(<span class="hljs-string">&apos;news&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) </span>{
  <span class="hljs-built_in">console</span>.log(data);
});
</code></pre>
<p>&#x5957;&#x63A5;&#x5B57;&#x5B9E;&#x9645;&#x4E0A;&#x7EE7;&#x627F;&#x4E86;<a href="https://github.com/component/emitter" target="_blank">Emitter</a>&#x7C7B;&#x7684;&#x6BCF;&#x4E2A;&#x65B9;&#x6CD5;&#xFF0C;&#x6BD4;&#x5982;<code>hasListeners</code>&#xFF0C;<code>once</code>&#x6216;<code>off</code>&#xFF08;&#x5220;&#x9664;&#x4E8B;&#x4EF6;&#x76D1;&#x542C;&#x5668;&#xFF09;&#x3002;</p>
<h4 id="socketcompressvalue">socket.compress(value)</h4>
<ul>
<li><code>value</code> <em>(Boolean)</em></li>
<li><strong>Returns</strong> <code>Socket</code></li>
</ul>
<p>&#x8BBE;&#x7F6E;&#x540E;&#x7EED;&#x4E8B;&#x4EF6;&#x53D1;&#x5C04;&#x7684;&#x4FEE;&#x9970;&#x7B26;&#xFF0C;&#x5982;&#x679C;&#x503C;&#x4E3A;&#x201C;true&#x201D;&#xFF0C;&#x4E8B;&#x4EF6;&#x6570;&#x636E;&#x5C06;&#x53EA;&#x6709;<em>compressed</em>&#x3002;&#x5F53;&#x60A8;&#x4E0D;&#x8C03;&#x7528;&#x8BE5;&#x65B9;&#x6CD5;&#x65F6;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A;&apos;true&apos;&#x3002;</p>
<pre><code class="lang-js">socket.compress(<span class="hljs-literal">false</span>).emit(<span class="hljs-string">&apos;an event&apos;</span>, { some: <span class="hljs-string">&apos;data&apos;</span> });
</code></pre>
<h4 id="socketclose">socket.close()</h4>
<ul>
<li><strong>Returns</strong> <code>Socket</code></li>
</ul>
<p>&#x624B;&#x52A8;&#x65AD;&#x5F00;&#x63D2;&#x5EA7;&#x3002;</p>
<h4 id="socketdisconnect">socket.disconnect()</h4>
<p>Synonym of <a href="#socketclose">socket.close()</a>.</p>
<h4 id="event-connect">Event: &apos;connect&apos;</h4>
<p>&#x5728;&#x5305;&#x62EC;&#x6210;&#x529F;&#x91CD;&#x65B0;&#x8FDE;&#x63A5;&#x7684;&#x8FDE;&#x63A5;&#x65F6;&#x89E6;&#x53D1;&#x3002;</p>
<h4 id="event-connecterror">Event: &apos;connect_error&apos;</h4>
<ul>
<li><code>error</code> <em>(Object)</em> error object</li>
</ul>
<p>&#x5728;&#x8FDE;&#x63A5;&#x9519;&#x8BEF;&#x65F6;&#x89E6;&#x53D1;&#x3002;</p>
<h4 id="event-connecttimeout">Event: &apos;connect_timeout&apos;</h4>
<p>&#x5728;&#x8FDE;&#x63A5;&#x8D85;&#x65F6;&#x65F6;&#x89E6;&#x53D1;&#x3002;</p>
<h4 id="event-error">Event: &apos;error&apos;</h4>
<ul>
<li><code>error</code> <em>(Object)</em> error object</li>
</ul>
<p>&#x53D1;&#x751F;&#x9519;&#x8BEF;&#x65F6;&#x89E6;&#x53D1;&#x3002;</p>
<h4 id="event-disconnect">Event: &apos;disconnect&apos;</h4>
<p>&#x5728;&#x65AD;&#x5F00;&#x65F6;&#x89E6;&#x53D1;&#x3002;</p>
<h4 id="event-reconnect">Event: &apos;reconnect&apos;</h4>
<ul>
<li><code>attempt</code> <em>(Number)</em> &#x91CD;&#x65B0;&#x8FDE;&#x63A5;&#x5C1D;&#x8BD5;&#x6B21;&#x6570;</li>
</ul>
<p>&#x5728;&#x6210;&#x529F;&#x91CD;&#x65B0;&#x8FDE;&#x63A5;&#x65F6;&#x6FC0;&#x53D1;&#x3002;</p>
<h4 id="event-reconnectattempt">Event: &apos;reconnect_attempt&apos;</h4>
<p>&#x5728;&#x5C1D;&#x8BD5;&#x91CD;&#x65B0;&#x8FDE;&#x63A5;&#x65F6;&#x6FC0;&#x53D1;&#x3002;</p>
<h4 id="event-reconnecting">Event: &apos;reconnecting&apos;</h4>
<ul>
<li><code>attempt</code> <em>(Number)</em> &#x91CD;&#x65B0;&#x8FDE;&#x63A5;&#x5C1D;&#x8BD5;&#x6B21;&#x6570;</li>
</ul>
<p>&#x5728;&#x6210;&#x529F;&#x91CD;&#x65B0;&#x8FDE;&#x63A5;&#x65F6;&#x6FC0;&#x53D1;&#x3002;</p>
<h4 id="event-reconnecterror">Event: &apos;reconnect_error&apos;</h4>
<ul>
<li><code>error</code> <em>(Object)</em> error object</li>
</ul>
<p>&#x5728;&#x91CD;&#x65B0;&#x8FDE;&#x63A5;&#x5C1D;&#x8BD5;&#x9519;&#x8BEF;&#x65F6;&#x89E6;&#x53D1;&#x3002;</p>
<h4 id="event-reconnectfailed">Event: &apos;reconnect_failed&apos;</h4>
<p>&#x5728;&#x201C;reconnectionAttempts&#x201D;&#x4E2D;&#x65E0;&#x6CD5;&#x91CD;&#x65B0;&#x8FDE;&#x63A5;&#x65F6;&#x89E6;&#x53D1;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="emit.html" class="navigation navigation-prev navigation-unique" aria-label="Previous page: emit">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"socket.io-client API","level":"1.4","depth":1,"previous":{"title":"emit","level":"1.3","depth":1,"path":"emit.md","ref":"emit.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"socket.io-client-api.md","mtime":"2017-03-06T17:38:26.605Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2017-03-07T13:45:59.536Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

